﻿<script> 
    $(function () {
        var _vm = new ViewModel();
        _vm.Init();
    });

    function ViewModel() {
        var me = this;

        //注意顺序
        me.DataTable = new PagedDataTable(me, "#table");
        me.DataTable.ShipObjs.push(@this.Raw(this.ViewBag.DepartmentListString));
        me.Dialog = new Dialog(me,"#frmEdit");
        me.app = new App(me, "#app");
        me.app.DeleteUrl = "@this.Href("~/SystemManage/Duty/Delete")";
        Extend.call(me);
    }
    function Extend() {
        var me = this;
         me.Init = function () {
            me.LoadModels();
        }
        me.LoadModels = function (page) {
            me.DataTable.SelectedModel = null;
            var data = me.app.SearchModel;
            data.page = page || 1;
            $hits.get("@this.Href("~/SystemManage/Duty/GetModels")", data, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
         );
         }
         me.OnSave = function () {
            var model = me.Dialog.Model;
            if (!$('#form1').formValid()) {
                return false;
            }
            if (me.Dialog.EditModel) {
                $hits.post("@this.Href("~/SystemManage/Duty/Update")", model, function (result) {
                    $hits.msg(result.Msg);
                    me.Dialog.Close();
                    me.LoadModels();
                }
               );
            }
            else {
                $hits.post("@this.Href("~/SystemManage/Duty/Add")", model, function (result) {
                    $hits.msg(result.Msg);
                    me.Dialog.Close();
                    me.LoadModels();
                }
             );
            }
        }
    }
</script>
